home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////////////
- // TRACEMSG Copyright 1995 Microsoft Systems Journal.
- // If this program works, it was written by Paul DiLascia.
- // If not, I don't know who wrote it.
- //
- // TRACEMSG implements message tracing for MFC CCmdTarget and CWnd classes.
- // CMsgTracer does the work. It's called from the template classes generated
- // by the definitions in TRACEMSG.H.
- //
- #include "stdafx.h"
- #ifdef _DEBUG // the whole file!
- #include "tracemsg.h" // message tracing templates
- #include "resource.h" // command IDs
- #include <dde.h> // for DDE messages
-
- IMPLEMENT_DYNAMIC(CMsgTracer, CCmdTarget)
-
- BEGIN_MESSAGE_MAP(CMsgTracer, CCmdTarget)
- ON_COMMAND(ID_TRACE_MSG, OnTraceMsg)
- ON_COMMAND(ID_TRACE_CMD, OnTraceCmd)
- ON_UPDATE_COMMAND_UI(ID_TRACE_MSG, OnUpdateTraceMsg)
- ON_UPDATE_COMMAND_UI(ID_TRACE_CMD, OnUpdateTraceCmd)
- END_MESSAGE_MAP()
-
- CMsgTracer theTracer; // one and only
-
- ////////////////
- // Maps ID (WM_ message or ID_ command) to name
- //
- struct MAPID {
- UINT nID; // ID_ or WM_ value
- LPCSTR lpszName; // name
- };
-
- #define MSG(WM_FOO) { WM_FOO, #WM_FOO },
- #define MFCMSG(WM_FOO) { WM_FOO, "MFC:"#WM_FOO }, // for MFC message
- #define APPMSG(WM_FOO) { WM_FOO, "APP:"#WM_FOO }, // for App message
-
- // Table of WM_ message names
- static const MAPID AllMessages[] = {
- MSG( WM_CREATE )
- MSG( WM_DESTROY )
- MSG( WM_MOVE )
- MSG( WM_SIZE )
- MSG( WM_ACTIVATE )
- MSG( WM_SETFOCUS )
- MSG( WM_KILLFOCUS )
- MSG( WM_ENABLE )
- MSG( WM_SETREDRAW )
- MSG( WM_SETTEXT )
- MSG( WM_GETTEXT )
- MSG( WM_GETTEXTLENGTH )
- MSG( WM_PAINT )
- MSG( WM_CLOSE )
- MSG( WM_QUERYENDSESSION )
- MSG( WM_QUIT )
- MSG( WM_QUERYOPEN )
- MSG( WM_ERASEBKGND )
- MSG( WM_SYSCOLORCHANGE )
- MSG( WM_ENDSESSION )
- MSG( WM_SHOWWINDOW )
- MSG( WM_CTLCOLORMSGBOX )
- MSG( WM_CTLCOLOREDIT )
- MSG( WM_CTLCOLORLISTBOX )
- MSG( WM_CTLCOLORBTN )
- MSG( WM_CTLCOLORDLG )
- MSG( WM_CTLCOLORSCROLLBAR )
- MSG( WM_CTLCOLORSTATIC )
- MSG( WM_WININICHANGE )
- MSG( WM_DEVMODECHANGE )
- MSG( WM_ACTIVATEAPP )
- MSG( WM_FONTCHANGE )
- MSG( WM_TIMECHANGE )
- MSG( WM_CANCELMODE )
- MSG( WM_SETCURSOR )
- MSG( WM_MOUSEACTIVATE )
- MSG( WM_CHILDACTIVATE )
- MSG( WM_QUEUESYNC )
- MSG( WM_GETMINMAXINFO )
- MSG( WM_ICONERASEBKGND )
- MSG( WM_NEXTDLGCTL )
- MSG( WM_SPOOLERSTATUS )
- MSG( WM_DRAWITEM )
- MSG( WM_MEASUREITEM )
- MSG( WM_DELETEITEM )
- MSG( WM_VKEYTOITEM )
- MSG( WM_CHARTOITEM )
- MSG( WM_SETFONT )
- MSG( WM_GETFONT )
- MSG( WM_QUERYDRAGICON )
- MSG( WM_COMPAREITEM )
- MSG( WM_COMPACTING )
- MSG( WM_NCCREATE )
- MSG( WM_NCDESTROY )
- MSG( WM_NCCALCSIZE )
- MSG( WM_NCHITTEST )
- MSG( WM_NCPAINT )
- MSG( WM_NCACTIVATE )
- MSG( WM_GETDLGCODE )
- MSG( WM_NCMOUSEMOVE )
- MSG( WM_NCLBUTTONDOWN )
- MSG( WM_NCLBUTTONUP )
- MSG( WM_NCLBUTTONDBLCLK )
- MSG( WM_NCRBUTTONDOWN )
- MSG( WM_NCRBUTTONUP )
- MSG( WM_NCRBUTTONDBLCLK )
- MSG( WM_NCMBUTTONDOWN )
- MSG( WM_NCMBUTTONUP )
- MSG( WM_NCMBUTTONDBLCLK )
- MSG( WM_KEYDOWN )
- MSG( WM_KEYUP )
- MSG( WM_CHAR )
- MSG( WM_DEADCHAR )
- MSG( WM_SYSKEYDOWN )
- MSG( WM_SYSKEYUP )
- MSG( WM_SYSCHAR )
- MSG( WM_SYSDEADCHAR )
- MSG( WM_KEYLAST )
- MSG( WM_INITDIALOG )
- MSG( WM_COMMAND )
- MSG( WM_SYSCOMMAND )
- MSG( WM_TIMER )
- MSG( WM_HSCROLL )
- MSG( WM_VSCROLL )
- MSG( WM_INITMENU )
- MSG( WM_INITMENUPOPUP )
- MSG( WM_MENUSELECT )
- MSG( WM_MENUCHAR )
- MSG( WM_ENTERIDLE )
- MSG( WM_MOUSEMOVE )
- MSG( WM_LBUTTONDOWN )
- MSG( WM_LBUTTONUP )
- MSG( WM_LBUTTONDBLCLK )
- MSG( WM_RBUTTONDOWN )
- MSG( WM_RBUTTONUP )
- MSG( WM_RBUTTONDBLCLK )
- MSG( WM_MBUTTONDOWN )
- MSG( WM_MBUTTONUP )
- MSG( WM_MBUTTONDBLCLK )
- MSG( WM_PARENTNOTIFY )
- MSG( WM_MDICREATE )
- MSG( WM_MDIDESTROY )
- MSG( WM_MDIACTIVATE )
- MSG( WM_MDIRESTORE )
- MSG( WM_MDINEXT )
- MSG( WM_MDIMAXIMIZE )
- MSG( WM_MDITILE )
- MSG( WM_MDICASCADE )
- MSG( WM_MDIICONARRANGE )
- MSG( WM_MDIGETACTIVE )
- MSG( WM_MDISETMENU )
- MSG( WM_CUT )
- MSG( WM_COPY )
- MSG( WM_PASTE )
- MSG( WM_CLEAR )
- MSG( WM_UNDO )
- MSG( WM_RENDERFORMAT )
- MSG( WM_RENDERALLFORMATS )
- MSG( WM_DESTROYCLIPBOARD )
- MSG( WM_DRAWCLIPBOARD )
- MSG( WM_PAINTCLIPBOARD )
- MSG( WM_VSCROLLCLIPBOARD )
- MSG( WM_SIZECLIPBOARD )
- MSG( WM_ASKCBFORMATNAME )
- MSG( WM_CHANGECBCHAIN )
- MSG( WM_HSCROLLCLIPBOARD )
- MSG( WM_QUERYNEWPALETTE )
- MSG( WM_PALETTEISCHANGING )
- MSG( WM_PALETTECHANGED )
- MSG( WM_DDE_INITIATE )
- MSG( WM_DDE_TERMINATE )
- MSG( WM_DDE_ADVISE )
- MSG( WM_DDE_UNADVISE )
- MSG( WM_DDE_ACK )
- MSG( WM_DDE_DATA )
- MSG( WM_DDE_REQUEST )
- MSG( WM_DDE_POKE )
- MSG( WM_DDE_EXECUTE )
- MSG( WM_DROPFILES )
- MSG( WM_POWER )
- MSG( WM_WINDOWPOSCHANGED )
- MSG( WM_WINDOWPOSCHANGING )
-
- // MFC specific messages
- MFCMSG( WM_QUERYAFXWNDPROC)
- MFCMSG( WM_SIZEPARENT)
- MFCMSG( WM_SETMESSAGESTRING)
- MFCMSG( WM_IDLEUPDATECMDUI)
- MFCMSG( WM_INITIALUPDATE)
- MFCMSG( WM_COMMANDHELP)
- MFCMSG( WM_HELPHITTEST)
- MFCMSG( WM_EXITHELPMODE)
- MFCMSG( WM_RECALCPARENT)
- MFCMSG( WM_SIZECHILD)
- MFCMSG( WM_KICKIDLE)
- MFCMSG( WM_QUERYCENTERWND)
- MFCMSG( WM_DISABLEMODAL)
- MFCMSG( WM_FLOATSTATUS)
- MFCMSG( WM_ACTIVATETOPLEVEL)
- MFCMSG( WM_QUERY3DCONTROLS)
- MFCMSG( WM_SOCKET_NOTIFY)
- MFCMSG( WM_SOCKET_DEAD)
-
- { 0, NULL, } // end of message list
- };
-
- // Table of common MFC command IDs
- static const BASED_CODE MAPID AllCommands[] = {
- MSG( ID_FILE_NEW )
- MSG( ID_FILE_OPEN )
- MSG( ID_FILE_CLOSE )
- MSG( ID_FILE_SAVE )
- MSG( ID_FILE_SAVE_AS )
- MSG( ID_FILE_PAGE_SETUP )
- MSG( ID_FILE_PRINT_SETUP )
- MSG( ID_FILE_PRINT )
- MSG( ID_FILE_PRINT_PREVIEW )
- MSG( ID_FILE_UPDATE )
- MSG( ID_FILE_SAVE_COPY_AS )
- MSG( ID_FILE_SEND_MAIL )
- MSG( ID_FILE_MRU_FIRST )
- MSG( ID_FILE_MRU_FILE1 )
- MSG( ID_FILE_MRU_FILE2 )
- MSG( ID_FILE_MRU_FILE3 )
- MSG( ID_FILE_MRU_FILE4 )
- MSG( ID_FILE_MRU_FILE5 )
- MSG( ID_FILE_MRU_FILE6 )
- MSG( ID_FILE_MRU_FILE7 )
- MSG( ID_FILE_MRU_FILE8 )
- MSG( ID_FILE_MRU_FILE9 )
- MSG( ID_FILE_MRU_FILE10 )
- MSG( ID_FILE_MRU_FILE11 )
- MSG( ID_FILE_MRU_FILE12 )
- MSG( ID_FILE_MRU_FILE13 )
- MSG( ID_FILE_MRU_FILE14 )
- MSG( ID_FILE_MRU_FILE15 )
- MSG( ID_FILE_MRU_FILE16 )
- MSG( ID_FILE_MRU_LAST )
- MSG( ID_EDIT_CLEAR )
- MSG( ID_EDIT_CLEAR_ALL )
- MSG( ID_EDIT_COPY )
- MSG( ID_EDIT_CUT )
- MSG( ID_EDIT_FIND )
- MSG( ID_EDIT_PASTE )
- MSG( ID_EDIT_PASTE_LINK )
- MSG( ID_EDIT_PASTE_SPECIAL )
- MSG( ID_EDIT_REPEAT )
- MSG( ID_EDIT_REPLACE )
- MSG( ID_EDIT_SELECT_ALL )
- MSG( ID_EDIT_UNDO )
- MSG( ID_EDIT_REDO )
- MSG( ID_VIEW_TOOLBAR )
- MSG( ID_VIEW_STATUS_BAR )
- MSG( ID_APP_ABOUT )
- MSG( ID_APP_EXIT )
- MSG( ID_WINDOW_NEW )
- MSG( ID_WINDOW_ARRANGE )
- MSG( ID_WINDOW_CASCADE )
- MSG( ID_WINDOW_TILE_HORZ )
- MSG( ID_WINDOW_TILE_VERT )
- MSG( ID_WINDOW_SPLIT )
- MSG( ID_INDICATOR_EXT )
- MSG( ID_INDICATOR_CAPS )
- MSG( ID_INDICATOR_NUM )
- MSG( ID_INDICATOR_SCRL )
- MSG( ID_INDICATOR_OVR )
- MSG( ID_INDICATOR_REC )
- MSG( ID_INDICATOR_KANA )
- MSG( AFX_IDW_CONTROLBAR_FIRST )
- MSG( AFX_IDW_CONTROLBAR_LAST )
- MSG( AFX_IDW_TOOLBAR )
- MSG( AFX_IDW_STATUS_BAR )
- MSG( AFX_IDW_PREVIEW_BAR )
- MSG( AFX_IDW_RESIZE_BAR )
- MSG( AFX_IDW_DOCKBAR_TOP )
- MSG( AFX_IDW_DOCKBAR_LEFT )
- MSG( AFX_IDW_DOCKBAR_RIGHT )
- MSG( AFX_IDW_DOCKBAR_BOTTOM )
- MSG( AFX_IDW_DOCKBAR_FLOAT )
- MSG( AFX_IDW_PANE_FIRST )
- MSG( AFX_IDW_PANE_LAST )
- MSG( AFX_IDW_HSCROLL_FIRST )
- MSG( AFX_IDW_VSCROLL_FIRST )
- MSG( AFX_IDW_SIZE_BOX )
- MSG( AFX_IDW_PANE_SAVE )
- MSG( ID_HELP_INDEX )
- MSG( ID_HELP_USING )
- MSG( ID_CONTEXT_HELP )
- MSG( ID_HELP )
- MSG( ID_DEFAULT_HELP )
- MSG( ID_NEXT_PANE )
- MSG( ID_PREV_PANE )
- MSG( ID_OLE_INSERT_NEW )
- MSG( ID_OLE_EDIT_LINKS )
- MSG( ID_OLE_EDIT_CONVERT )
- MSG( ID_OLE_EDIT_CHANGE_ICON )
- MSG( ID_OLE_EDIT_PROPERTIES )
- MSG( ID_OLE_VERB_FIRST )
- MSG( ID_OLE_VERB_LAST )
- MSG( AFX_ID_PREVIEW_CLOSE )
- MSG( AFX_ID_PREVIEW_NUMPAGE )
- MSG( AFX_ID_PREVIEW_NEXT )
- MSG( AFX_ID_PREVIEW_PREV )
- MSG( AFX_ID_PREVIEW_PRINT )
- MSG( AFX_ID_PREVIEW_ZOOMIN )
- MSG( AFX_ID_PREVIEW_ZOOMOUT )
- MSG( ID_RECORD_FIRST )
- MSG( ID_RECORD_LAST )
- MSG( ID_RECORD_NEXT )
- MSG( ID_RECORD_PREV )
-
- // CMDLEARN's IDs
- APPMSG( ID_UPDATE_STATUS )
- APPMSG( ID_VIEW_COMBO_DIALOG )
- APPMSG( ID_TRACE_MSG )
- APPMSG( ID_TRACE_CMD )
-
- { 0, NULL }
- };
-
- CMsgTracer::CMsgTracer()
- {
- ASSERT(this==&theTracer); // only one global object allowed
-
- m_szIndent[0]= 0;
- m_nCallDepth = 0;
- m_bTraceCmd=TRUE;
- m_bTraceMsg=TRUE;
-
- // Initialize WM_ message name table
- for (const MAPID* lpMM = AllMessages; lpMM->nID != 0; lpMM++)
- m_mapMsg[lpMM->nID] = (void*)lpMM->lpszName;
-
- // Initialize ID_ command name table
- for (lpMM = AllCommands; lpMM->nID; lpMM++)
- m_mapCmd[lpMM->nID] = (void*)lpMM->lpszName;
-
- // open trace file
- TRY {
- m_file.Open("TRACE.OUT",
- CFile::modeWrite | CFile::modeCreate | CFile::shareDenyWrite);
- } CATCH_ALL(e) {
- MessageBox(NULL, "Failed to open trace file.", NULL,
- MB_OK|MB_ICONEXCLAMATION);
- MessageBeep(MB_ICONEXCLAMATION);
- } END_CATCH_ALL
- }
-
- CMsgTracer::~CMsgTracer()
- {
- m_file.Close(); // write to disk
- }
-
- //////////////////
- // Write string to trace file
- //
- void CMsgTracer::Write(LPCSTR buf, int len)
- {
- if (m_file.m_hFile)
- m_file.Write(buf, len >= 0 ? len : strlen(buf));
- }
-
- //////////////////
- // Trace OnCmdMsg. Returns FALSE if not traced.
- //
- BOOL CMsgTracer::TRACE_OnCmdMsg(CCmdTarget* pTarg, UINT nID, int nCode,
- void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
- {
- if (!m_bTraceCmd)
- return FALSE;
-
- // print pExtra or "NULL"
- char p1[20] = "NULL";
- if (pExtra)
- sprintf(p1,"0x%p", pExtra);
-
- // print pHandlerInfo or "NULL"
- char p2[20] = "NULL";
- if (pHandlerInfo)
- sprintf(p2,"0x%p", pHandlerInfo);
-
- // Write diagnostic
- char buf[255];
- int len = sprintf(buf,
- "%s%s[%p]::OnCmdMsg(%s, %s, %s, %s)\n",
- m_szIndent,
- pTarg->GetRuntimeClass()->m_lpszClassName, pTarg,
- GetCommandName(nID),
- GetCmdCodeName(nCode),
- p1,
- p2);
- Write(buf, len);
-
- PushCallLevel(); // increment indent for pretty-printing
- return TRUE; // message traced
- }
-
- // Don't show these messages, they clutter the file
- static UINT SkipMessages[] = {
- WM_MOUSEMOVE,
- WM_NCMOUSEMOVE,
- WM_NCHITTEST,
- WM_SETCURSOR,
- WM_CTLCOLOR,
- WM_ENTERIDLE,
- WM_CANCELMODE,
- 0 // end of table
- };
-
- //////////////////
- // Trace a WindowProc message. Returns FALSE if not traced.
- //
- BOOL CMsgTracer::TRACE_WindowProc(CWnd* pWnd, UINT msg, WPARAM wp, LPARAM lp)
- {
- if (msg==WM_NCCREATE || msg==WM_NCDESTROY) {
- // Always note window creation/destruction
- char buf[80];
- int len = sprintf(buf,
- "%s*** %s %s at %p ***\n",
- m_szIndent,
- msg==WM_NCCREATE ? "Created" : "Destroyed",
- pWnd->GetRuntimeClass()->m_lpszClassName,
- pWnd);
- Write (buf, len);
- }
-
- // Only trace of tracing is on.
- if (!(m_bTraceMsg ||
- (m_bTraceCmd &&
- (msg==WM_COMMAND || msg==WM_NOTIFY || msg==WM_IDLEUPDATECMDUI))))
- return FALSE;
-
- // Don't report frequently sent messages
- for (UINT* lpMessage = SkipMessages; *lpMessage != 0; lpMessage++) {
- if (*lpMessage == msg)
- return FALSE;
- }
-
- // Write diagnostic message
- char buf[255];
- int len = sprintf(buf,
- "%s%s[%p]::WindowProc(%s, 0x%04x, 0x%08lx)\n",
- m_szIndent,
- pWnd->GetRuntimeClass()->m_lpszClassName,
- pWnd,
- GetMessageName(msg),
- wp,
- lp);
- Write(buf, len);
-
- PushCallLevel(); // increment indent for pretty-printing
- return TRUE; // message traced
- }
-
- //////////////////
- // Trace OnCommand. Returns FALSE if not traced.
- //
- BOOL CMsgTracer::TRACE_OnCommand(CWnd* pWnd, WPARAM wp, LPARAM lp)
- {
- if (!m_bTraceCmd)
- return FALSE;
-
- // Write diagnostic message
- char buf[255];
- int len = sprintf(buf,
- "%s%s[%p]::OnCommand(%s, 0x%08lx)\n",
- m_szIndent,
- pWnd->GetRuntimeClass()->m_lpszClassName,
- pWnd,
- GetCommandName(wp),
- lp);
- Write(buf, len);
-
- PushCallLevel(); // increment indent for pretty-printing
- return TRUE; // traced
- }
-
- /////////////////
- // Trace return value from OnCmdMsg, OnCommand or WindowProc.
- //
- void CMsgTracer::TRACE_ReturnOnCmdMsg(BOOL bRet)
- {
- PopCallLevel();
- char buf[80];
- int len = sprintf(buf, "%s%s\n", m_szIndent, bRet ? "TRUE" : "FALSE");
- Write(buf, len);
- }
-
- void CMsgTracer::TRACE_ReturnOnCommand(BOOL bRet)
- {
- PopCallLevel();
- char buf[80];
- int len = sprintf(buf, "%s%s\n", m_szIndent, bRet ? "TRUE" : "FALSE");
- Write(buf, len);
- }
-
- void CMsgTracer::TRACE_ReturnWindowProc(LRESULT lRet)
- {
- PopCallLevel();
- char buf[80];
- int len = sprintf(buf, "%s0x%08x\n", m_szIndent, lRet);
- Write(buf, len);
- }
-
- ////////////////
- // Get message name from ID.
- //
- LPCSTR CMsgTracer::GetMessageName(UINT msg)
- {
- // Use MFC map to improve performance looking up
- // (hash is faster than linear search).
- //
- static char szBuf[80]; // scratch buffer
-
- if (msg >= 0xC000) {
- // Window message registered with 'RegisterWindowMessage'
- // (actually a USER atom)
- if (::GetClipboardFormatName(msg, szBuf, sizeof(szBuf)) != 0)
- return szBuf;
-
- } else if (msg >= WM_USER) {
- // WM_USER + x message
- sprintf(szBuf, "WM_USER+0x%04x", msg - WM_USER);
- return szBuf;
-
- }
-
- // Look in name table
- LPCSTR lpsz = (LPCSTR)m_mapMsg[msg];
- if (lpsz)
- return lpsz;
-
- // Unnamed message ID
- sprintf(szBuf, "0x%04x", msg);
- return szBuf;
- }
-
- //////////////////
- // Get command name from ID
- //
- LPCSTR CMsgTracer::GetCommandName(UINT nID)
- {
- // find message name
- if (nID >= 0x8000) {
- LPCSTR lpsz = (LPCSTR)m_mapCmd[nID];
- if (lpsz)
- return lpsz;
- }
- static char szBuf[80];
- sprintf(szBuf, "%d", nID);
- return szBuf;
- }
-
- //////////////////
- // Get command code name (CN_COMMAND, etc) from ID.
- //
- LPCSTR CMsgTracer::GetCmdCodeName(int nCode)
- {
- if (nCode==CN_COMMAND)
- return "CN_COMMAND";
- else if (nCode==CN_UPDATE_COMMAND_UI)
- return "CN_UPDATE_COMMAND_UI";
-
- static char szBuf[80];
- sprintf(szBuf, "%d", nCode);
- return szBuf;
- }
-
- //////////////////
- // Handle Trace On/Off (messages)
- //
- void CMsgTracer::OnTraceMsg()
- {
- m_bTraceMsg = !m_bTraceMsg;
- char buf[80];
- int len = sprintf(buf, "MESSAGE TRACING %s\n", m_bTraceMsg ? "ON" : "OFF");
- Write(buf, len);
- }
-
- //////////////////
- // Handle Trace On/Off (commands)
- //
- void CMsgTracer::OnTraceCmd()
- {
- m_bTraceCmd = !m_bTraceCmd;
- char buf[80];
- int len = sprintf(buf, "COMMAND TRACING %s\n", m_bTraceCmd ? "ON" : "OFF");
- Write(buf, len);
- }
-
- void CMsgTracer::OnUpdateTraceMsg(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bTraceMsg);
- }
-
- void CMsgTracer::OnUpdateTraceCmd(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(m_bTraceCmd);
- }
-
- #endif
-